package cz.results.winter6.sestavy;

//Java
import cz.results.winter6.io.IoConfig;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

//JAXP
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.sax.SAXResult;


//Avalon
import org.apache.avalon.framework.ExceptionUtil;
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;

//FOP
import org.apache.fop.apps.Driver;
import org.apache.fop.apps.FOPException;
import org.apache.fop.messaging.MessageHandler;

/** slouzi ke konverzi souboru ve formatu xml na pdf */
public class XML2PDF {
    
    /** staticka metoda, ktera provadi vlastni konverzi filu xml na pdf
     * @param xml vstupni soubor ve formatu xml
     * @param xslt vstupni soubor xslt obsahujici sablonu pro transformaci xml na pdf
     * @param pdf vystupni soubor
     */
    public static void convertXML2PDF(File xml, File xslt, File pdf)
    throws IOException, FOPException, TransformerException {
        //Construct driver
        Driver driver = new Driver();
        
        //nacteni externi konfigurace - !!!! osetrit cestu k souboru
        //File userConfigFile = new File("h:/popelnice/conf/", "userconfig.xml");
        //Options options = new Options(userConfigFile);
        
        //Setup logger
        Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
        driver.setLogger(logger);
        MessageHandler.setScreenLogger(logger);
        
        //Setup Renderer (output format)
        driver.setRenderer(Driver.RENDER_PDF);
        
        //Setup output
        OutputStream out = new java.io.FileOutputStream(pdf);
        try {
            driver.setOutputStream(out);
            
            //Setup XSLT
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer(new StreamSource(xslt));
            
            //Setup input for XSLT transformation
            Source src = new StreamSource(xml);
            
            //Resulting SAX events (the generated FO) must be piped through to FOP
            Result res = new SAXResult(driver.getContentHandler());
            
            //Start XSLT transformation and FOP processing
            transformer.transform(src, res);
        } finally {
            out.close();
        }
    }
    
    public static void ShowPDF(String pdffile) throws Exception {
        Process p = Runtime.getRuntime().exec("acroread " + pdffile);
        //p.waitFor(); 
    }
    
    
    public static void main(String[] args) {
        try {
        
            XML2PDF.convertXML2PDF(new File(IoConfig.dataPath+"startovka.xml"),
                    new File(IoConfig.dataPath+"startovka2fo4.xsl"),
                    new File(IoConfig.dataPath+"startovka.pdf"));
            XML2PDF.ShowPDF(IoConfig.dataPath+"startovka.pdf");   
       
        } catch (Exception e) {
            System.err.println(ExceptionUtil.printStackTrace(e));
            System.exit(-1);
        }
    }
}

